﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightClientEditor;
using GameEngine.Model;
using GameEngine.Clients.Silverlight;
using GameEditor.View;
using GameEngine.Model.Data;
using PureMVC.Patterns;

namespace ExampleSilverlight
{
    public partial class GameMenuUC : UserControl
    {
        #region Child Windows      
        ChildWindow mapPropertiesEditorWindow = new ChildWindow()
        {
            Title = "Map Properties"
        };
        ChildWindow resourcePropertiesEditorWindow = new ChildWindow()
        {
            Title = "Resource Properties"
        };
        ChildWindow shapePropertiesEditorWindow = new ChildWindow()
        {
            Title = "Shape Properties"
        };
        ChildWindow thingPropertiesEditorWindow = new ChildWindow()
        {
            Title = "Thing Properties"
        };
        ChildWindow thingTypePropertiesEditorWindow = new ChildWindow()
        {
            Title = "ThingType Properties"
        };
        ChildWindow viewPropertiesEditorWindow = new ChildWindow()
        {
            Title = "View Properties"
        };
        ChildWindow worldPropertiesEditorWindow = new ChildWindow()
        {
            Title = "World Properties"
        };
        #endregion

        ThingTypePropertiesEditor_MenuMediator<int> thingTypeEditorMed = null;
        WorldPropertiesEditor_MenuMediator<int> worldEditorMed;
        MapPropertiesEditor_Mediator<int> mapEditorMed;

        public GameMenuUC(IGameDataProxy<int> gameDP)
        {
            InitializeComponent();
            #region ChildWindow closing disappear window
            mapPropertiesEditorWindow.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(
                delegate(object sender, System.ComponentModel.CancelEventArgs e)
                {
                    LayoutRoot.Children.Remove(mapPropertiesEditorWindow);
                });
            resourcePropertiesEditorWindow.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(
                delegate(object sender, System.ComponentModel.CancelEventArgs e)
                {
                    LayoutRoot.Children.Remove(resourcePropertiesEditorWindow);
                });
            shapePropertiesEditorWindow.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(
                delegate(object sender, System.ComponentModel.CancelEventArgs e)
                {
                    LayoutRoot.Children.Remove(shapePropertiesEditorWindow);
                });
            thingPropertiesEditorWindow.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(
                delegate(object sender, System.ComponentModel.CancelEventArgs e)
                {
                    LayoutRoot.Children.Remove(thingPropertiesEditorWindow);
                });
            thingTypePropertiesEditorWindow.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(
                delegate(object sender, System.ComponentModel.CancelEventArgs e)
                {
                    LayoutRoot.Children.Remove(thingTypePropertiesEditorWindow);
                });
            viewPropertiesEditorWindow.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(
                delegate(object sender, System.ComponentModel.CancelEventArgs e)
                {
                    LayoutRoot.Children.Remove(viewPropertiesEditorWindow);
                });
            worldPropertiesEditorWindow.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(
                delegate(object sender, System.ComponentModel.CancelEventArgs e)
                {
                    LayoutRoot.Children.Remove(worldPropertiesEditorWindow);
                });
            #endregion
            //game, level

            GameContainer.Children.Add(SampleSilverlightViewFacade.Helper.GetGameView() as FrameworkElement);

            //TODO init editor facade
            SilverlightEditorFacade editor = SilverlightEditorFacade.Instance;

            //TODO gameEditor
            //GamePropertiesEditor_MenuMediator<int> gameEditorMed = SilverlightEditorViewFacade.Helper.CreateGamePropertiesEditor(gameDP);
            //GameContainer.Children.Add(gameEditorMed.ViewComponent as FrameworkElement);

            //TODO levelEditor
            //LevelPropertiesEditor_MenuMediator<int> levelEditorMed = SilverlightEditorViewFacade.Helper.CreateLevelPropertiesEditor(gameDP.CurrentLevel);
            //GameContainer.Children.Add(levelEditorMed.ViewComponent as FrameworkElement);

            /*ShapePropertiesEditor_MenuMediator<int> shapeEditorMed = SilverlightEditorViewFacade.Helper.CreateShapePropertiesEditor();
            ViewPropertiesEditor_MenuMediator<int> viewEditorMed = SilverlightEditorViewFacade.Helper.CreateViewPropertiesEditor();
            shapePropertiesEditorWindow.Content = shapeEditorMed.ViewComponent as FrameworkElement;
            viewPropertiesEditorWindow.Content = viewEditorMed.ViewComponent as FrameworkElement;

            ThingPropertiesEditor_MenuMediator<int> thingEditorMed = SilverlightEditorViewFacade.Helper.CreateThingEditorMenu();            
            thingPropertiesEditorWindow.Content = thingEditorMed.ViewComponent as FrameworkElement;*/

            thingTypeEditorMed = SilverlightEditorViewFacade.Helper.CreateThingTypePropertiesEditor();
            thingTypePropertiesEditorWindow.Content = thingTypeEditorMed.ViewComponent as FrameworkElement;
            //ResourcePropertiesEditor_MenuMediator<int> resourceEditorMed = SilverlightEditorViewFacade.Helper.CreateResourceEditorMenu();
            // resourcePropertiesEditorWindow.Content = resourceEditorMed.ViewComponent as FrameworkElement;

            //TODO world selector
            var worldId = gameDP.CurrentLevel.VO.Worlds[0].Id;
            selectWorld(worldId);
        }
        
        private void selectWorld(IWorldVOId worldId)
        {
            var worldDP = SilverlightModelFacade.Helper.GetWorldDataProxy(worldId);
            var mapMed = SilverlightViewFacade.Helper.GetMapMediator(worldDP.Map);
            mapMed.MouseOnItems(true);
            //TODO unregister
            mapMed.RegisterMapNavigator(MapNavigator);
            mapMed.RegisterMapZoom(MapZoom);
            mapMed.OnItemSelected += new Action<IThingDataProxy<int>>(mapMed_OnItemSelected);

            worldEditorMed = SilverlightEditorViewFacade.Helper.CreateWorldPropertiesEditor(worldDP);
            mapEditorMed = SilverlightEditorViewFacade.Helper.CreateMapPropertiesEditor(worldDP.Map);

            //TODO erro de parent already set das views
            worldPropertiesEditorWindow.Content = worldEditorMed.ViewComponent as FrameworkElement;
            mapPropertiesEditorWindow.Content = mapEditorMed.ViewComponent as FrameworkElement;
        }

        void mapMed_OnItemSelected(IThingDataProxy<int> obj)
        {
            ThingPropertiesEditor_MenuMediator<int> thingEditorMed = SilverlightEditorViewFacade.Helper.CreateThingEditorMenu(obj); 
            thingPropertiesEditorWindow.Content = thingEditorMed.ViewComponent as FrameworkElement;
            LayoutRoot.Children.Remove(thingPropertiesEditorWindow);
            LayoutRoot.Children.Add(thingPropertiesEditorWindow);
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            MyFacade.Instance.saveGame();
        }

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            SilverlightApplicationFacadeHelper.DesktopNavigate(new MainMenuUC());
            MyFacade.Instance.RemoveMediator(thingTypeEditorMed.MediatorName);
            MyFacade.Instance.RemoveMediator(worldEditorMed.MediatorName);
            MyFacade.Instance.RemoveMediator(mapEditorMed.MediatorName);
            SilverlightApplicationFacadeHelper.DestroyGame();
            
        }

        private void InterfaceNavigator_Click(object sender, RoutedEventArgs e)
        {
            switch (MapNavigator.Visibility)
            {
                case System.Windows.Visibility.Collapsed:
                    MapNavigator.Visibility = System.Windows.Visibility.Visible;
                    break;
                case System.Windows.Visibility.Visible:
                    MapNavigator.Visibility = System.Windows.Visibility.Collapsed;
                    break;
            }
        }

        private void InterfaceZoomBar_Click(object sender, RoutedEventArgs e)
        {
            switch (MapZoom.Visibility)
            {
                case System.Windows.Visibility.Collapsed:
                    MapZoom.Visibility = System.Windows.Visibility.Visible;
                    break;
                case System.Windows.Visibility.Visible:
                    MapZoom.Visibility = System.Windows.Visibility.Collapsed;
                    break;
            }
        }

        private void MapGlobalProperties_Click(object sender, RoutedEventArgs e)
        {
            LayoutRoot.Children.Remove(mapPropertiesEditorWindow);
            LayoutRoot.Children.Add(mapPropertiesEditorWindow);
        }

        private void WorldProperties_Click(object sender, RoutedEventArgs e)
        {
            LayoutRoot.Children.Remove(worldPropertiesEditorWindow);
            LayoutRoot.Children.Add(worldPropertiesEditorWindow);
        }

        private void ThingTypeProperties_Click(object sender, RoutedEventArgs e)
        {
            LayoutRoot.Children.Remove(thingTypePropertiesEditorWindow);
            LayoutRoot.Children.Add(thingTypePropertiesEditorWindow);
        }

        private void ResourceProperties_Click(object sender, RoutedEventArgs e)
        {
            LayoutRoot.Children.Remove(resourcePropertiesEditorWindow);
            LayoutRoot.Children.Add(resourcePropertiesEditorWindow);
        }
    }
}
